// @author A0096579E

package rightnow.util;

import java.util.Comparator;

import org.joda.time.DateTime;

import rightnow.storage.Task;

/**
 * StartEndComparator imposes ordering of Tasks based on their start and end
 * attributes. Tasks are compared chronologically with the starting point
 * taking priority in the comparison over the ending point.
 */
public class StartEndComparator implements Comparator<Task> {

	@Override
	public int compare(Task taskA, Task taskB) {
		DateTime startA = taskA.getStart();
		DateTime endA = taskA.getEnd();
		DateTime startB = taskB.getStart();
		DateTime endB = taskB.getEnd();
		
		if (startA != null) {
			if (startB != null) {
				int compareResult_SASB = startA.compareTo(startB);
				if (compareResult_SASB == 0 && endA != null && endB != null) {
					return endA.compareTo(endB);
				} else {
					return compareResult_SASB;
				}
			} else if (endB != null) {
				return startA.compareTo(endB);
			} else {
				return -1;
			}
		} else if (endA != null) {
			if (startB != null) {
				return endA.compareTo(startB);
			} else if (endB != null) {
				return endA.compareTo(endB);
			} else {
				return -1;
			}
		} else if (startB != null || endB != null) {
			return 1;
		} else { // if startB and endB are null
			return 0;
		}
		
	}

}
